package by.ostrovsky.webhousingsystem.dao.implementation;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import by.ostrovsky.webhousingsystem.dao.implementation.idao.IOperateDAO;
import by.ostrovsky.webhousingsystem.dao.implementation.idao.IReadDAO;
import by.ostrovsky.webhousingsystem.exception.CommonException;
import by.ostrovsky.webhousingsystem.exception.OperationErrorException;
import by.ostrovsky.webhousingsystem.hibernate.HibernateUtil;
import by.ostrovsky.webhousingsystem.hibernate.entity.Account;

public class MySQLAccountDAO implements IOperateDAO<List<Account>, Boolean>,
		IReadDAO<Account> {
	private static Logger logger = Logger.getLogger(MySQLAccountDAO.class);

	@Override
	public Boolean update(List<Account> object) {
		return false;
	}

	@Override
	public Boolean create(List<Account> account) throws OperationErrorException {
		try {
			Session session = HibernateUtil.getSessionFactory().openSession();
			session.beginTransaction();
			for (Account acc : account) {
				try {
					session.save(acc);
				} catch (HibernateException ex) {
					logger.error("account with login: " + acc.getId()
							+ " status: " + ex);
				}
			}
			session.getTransaction().commit();
			session.close();
			return true;
		} catch (HibernateException e) {
			logger.error(e);
			throw new OperationErrorException();
		}
	}

	@Override
	public Boolean delete(List<Account> account) throws OperationErrorException {
		try {
			Session session = HibernateUtil.getSessionFactory().openSession();
			session.beginTransaction();
			for (Account acc : account) {
				session.delete(acc);
			}
			session.getTransaction().commit();
			session.close();
			return true;
		} catch (HibernateException e) {
			logger.error(e);
			throw new OperationErrorException();
		}
	}

	@Override
	public List<Account> readAll() throws CommonException {
		try {
			Session session = HibernateUtil.getSessionFactory().openSession();
			session.beginTransaction();
			@SuppressWarnings("unchecked")
			List<Account> list = session.createCriteria(Account.class).list();
			session.getTransaction().commit();
			session.close();

			return list;
		} catch (HibernateException e) {
			logger.error(e);
			throw new OperationErrorException();
		}
	}

	@Override
	public Account read(Account account) throws CommonException {
		try {
			Session session = HibernateUtil.getSessionFactory().openSession();
			session.beginTransaction();
			@SuppressWarnings("unchecked")
			List<Account> list = session.createCriteria(Account.class)
					.add(Restrictions.like("id", account.getId())).list();
			
			Account acc = new Account();
			if (list.size() != 0){
				acc = list.get(0);
				return acc;
			}
			else
				throw new NullPointerException();
		} catch (HibernateException e) {
			logger.error(e);
			throw new OperationErrorException();
		}
	}
}
